<?php
namespace App\Admin\Controllers\ApiSys;

use App\Http\Controllers\Controller;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\SupplierM;
use App\Models\Product\ProductMaterialApplicationClassify;
use App\Models\Product\ProductMaterialClassifyMax;
use App\Models\Product\ProductMaterialClassifyMin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BasicsController extends Controller{

    public function countries(Request $request){
        $q = $request->get('q');
        return AmazonCountry::query()
            ->where('aws_region',  $q)
            ->get()
            ->map(function ($model){
                return collect(['id' => $model->country_id,
                    'text' => $model->country->country ?? '']);
            });
    }

    //材料大类
    public function classifyMax(Request $request){
        $q = $request->get('q');
        return ProductMaterialClassifyMax::query()
            ->where('id', $q)
            ->get(['id', DB::raw('code as text')]);
    }

    //材料小类
    public function classifyMin(Request $request){
        $q = $request->get('q');
        return ProductMaterialClassifyMin::query()
            ->where('parent_id', $q)
            ->get(['id', DB::raw('title as text')]);
    }

    //材料供应商
    public function materialSupplierList(Request $request)
    {
        $q = $request->get('q');
        return SupplierM::query()
            ->where('short_name', 'like', "%$q%")
            ->where('type', SupplierM::TYPE_MATERIAL)
            ->where('audit_status', SupplierM::AUDIT_SUCCESS)
            ->where('status', SupplierM::STATUS_YES)
            ->paginate(null, ['id', 'short_name as text']);
    }

}
